import {ConversationType} from "./ConversationType.js"
import { Group } from "./Group.js"
import { User } from "./User.js"
import DC from "@/DC/index.js"
export class Conversation {
	
    id='' 
    type = ConversationType.Single
    sid='' 
    avatar='' 
    name='' 
    unread = 0 
    lastMessage = '' 
    lastTime = null
    isTop = false 
    isMute = false 
	mention = '' 
	mentionAll = false 
	remarks = '' 
	clearTime = 0 
	
	
	
	_target = null 
	
	
	
	equal(conv){
	    if(!conv){
	        return false
	    }
	    return this.type === conv.type && this.id === conv.id;
	}
	
	getSaveInfo(){
		const {_target,...rest} = this
		return rest
	}
	
	initTarget(){
		if(this.type==ConversationType.Single){
			this._target = DC.userDB.getUser(this.id)
		}
		if(this.type==ConversationType.Group){
			this._target = DC.groupDB.getGroup(this.id)
		}
	}
	
	
	
    static biz2Type(bizType){
        if(bizType=='群'){
            return ConversationType.Group
        }
        if(bizType=='用户'){
            return ConversationType.Single
        }
		return null
    }
	
	static type2Biz(type){
	    if(type==ConversationType.Group){
	        return '群'
	    }
	    if(type==ConversationType.Single){
	        return '用户'
	    }
		return ''
	}
	
	
	static mix(conversation,plain){
	    Object.assign(conversation,plain)
		return conversation
	}
    
    static convertFromAPI(conversation,plain){
        conversation.sid = plain.id
        conversation.id = plain.toBizId
        conversation.type = Conversation.biz2Type(plain.toBizType)
        conversation.avatar = plain.avatarUrl
        conversation.name = plain.userName
        conversation.unread = plain.unreadCount||0
        conversation.lastMessage = plain.lastMsgContent
        conversation.lastTime = plain.lastMsgDate
        conversation.isTop = plain.isTop=='1'
        conversation.isMute = plain.isDisturb=='1'
        conversation.mention = plain.atMsgId||''
		conversation.mentionAll = plain.atMsgAll=='true'
		conversation.remarks = plain.remarks
		conversation.clearTime = plain.delMsgTime
		return conversation
    }
	
	static createNullConversation(id,type){
		const conversation = new Conversation()
		conversation.id = id
		conversation.type = type
		return conversation
	}
	
}